﻿'******************************************************
'******************************************************
'Application Title: Google Widget Editor
'version: 1.0
'Date: 01-02-2008
'Requirements: win2k or later
'Author: Andrew Pye
'Author Email: StretchMasterP@googlemail.com
'License: Released under GNU GENERAL PUBLIC LICENSE v3
'ToDo:
'Changes:Imports System.Text
'******************************************************
'******************************************************

Imports System.Xml
Imports System.Xml.Serialization

Public Class CodeTree
    Public Event NodeSelected(ByRef Node As CodeStore)
    Public Event NodeDoubleClicked(ByRef Node As CodeStore)
    Private o_CodeStoreCollection As CodeStoreCollection
    Private Sub OnNodeDoubleClicked(ByRef Node As CodeStore)
        RaiseEvent NodeDoubleClicked(Node)
    End Sub
    Private Sub OnNodeSelected(ByRef Node As CodeStore)
        RaiseEvent NodeSelected(Node)
    End Sub
    Private Sub LoadItems(ByVal filename As String)
        Dim string_reader As IO.StringReader
        Dim Tstr As String = My.Computer.FileSystem.ReadAllText(filename)
        Dim xml_serializer As New XmlSerializer(GetType(CodeStoreCollection))
        ' Make a StringReader holding the serialization.
        string_reader = New IO.StringReader(Tstr)
        ' Create the new Person object from the serialization.
        o_CodeStoreCollection = xml_serializer.Deserialize(string_reader)

    End Sub
    Public Sub LoadData(ByVal FileName As String)
        ' Dim o_Code As CodeSnippet = Nothing
        Dim CurrNode As TreeNode = Nothing
        ' Dim m_xmld As New XmlDocument()
        ' Dim m_nodelist As XmlNodeList
        Me.ImageList = Me.ImageList1
        ' m_xmld.Load(FileName)
        LoadItems(FileName)
        'Me.o_CodeStoreCollection = New CodeStoreCollection
        'Me.o_CodeStoreCollection.Add("b", "a.b", "b", "b", CodeSnippet.enumScope.Public)
        'Me.o_CodeStoreCollection.Save(FileName)
        RefreshData()
        'Me.Nodes.Clear()
        'Me.Nodes.Add("Code", "Code")

        '' m_nodelist = m_xmld.GetElementsByTagName("Class")

        'For Each XmlNode As CodeStore In Me.o_CodeStoreCollection

        '    Dim TNameSpace() As String = XmlNode.Name_Space.Split(".")
        '    CurrNode = Me.Nodes("Code")
        '    For Each TName As String In TNameSpace
        '        CurrNode = AddNode(CurrNode, TName)
        '        CurrNode.ImageKey = "OPEN"
        '    Next
        '    CurrNode = AddNode(CurrNode, XmlNode.Name)

        '    CurrNode.Tag = XmlNode
        '    CurrNode.ImageKey = "CODE"
        'Next


    End Sub
    Public Sub RefreshData()
        Dim CurrNode As TreeNode = Nothing
        Me.Nodes.Clear()
        Me.Nodes.Add("Code", "Code")
        For Each XmlNode As CodeStore In Me.o_CodeStoreCollection

            Dim TNameSpace() As String = XmlNode.Name_Space.Split(".")
            CurrNode = Me.Nodes("Code")
            For Each TName As String In TNameSpace
                CurrNode = AddNode(CurrNode, TName)
                CurrNode.ImageKey = "OPEN"
            Next
            CurrNode = AddNode(CurrNode, XmlNode.Name)

            CurrNode.Tag = XmlNode
            CurrNode.ImageKey = "CODE"
        Next
    End Sub
    Public Function AddNode(ByVal CurrentNode As TreeNode, ByVal Name As String) As TreeNode
        If CurrentNode.Nodes.ContainsKey(Name) Then
            Return CurrentNode.Nodes(Name)
        Else
            Return CurrentNode.Nodes.Add(Name.ToUpper, Name)
        End If
    End Function
    Private Sub CodeTree_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
        Dim TNode As TreeNode = Me.SelectedNode
        If TNode IsNot Nothing Then
            If TNode.Tag IsNot Nothing Then
                If TypeOf TNode.Tag Is CodeStore Then
                    Me.OnNodeDoubleClicked(TNode.Tag)
                End If
            End If
        End If
    End Sub
    Private Sub CodeTree_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles Me.NodeMouseClick
        If e.Node.Tag IsNot Nothing Then
            If TypeOf e.Node.Tag Is CodeStore Then
                OnNodeSelected(e.Node.Tag)
            End If
        End If

    End Sub
    Public ReadOnly Property CodeStoreCollection() As CodeStoreCollection
        Get
            Return o_CodeStoreCollection
        End Get
    End Property
End Class
